import Frame from "../frameNamespace";
import gameMode_Json, { GAME_MODE } from "../other/game/gameMode_Json";
import UMe, { sers } from "../system/OverallSystem ";

export interface GamModeProperty {
    id: number,
    /** 场景名 */
    sceneName: string,
    /** 所需资源的bundle名 */
    bundleName: string,
    /** 描述 */
    explain: string;
    /** 该模式的bundle */
    bundle?: cc.AssetManager.Bundle;
    /** 视频解锁次数 */
    videoCnt: number;
}

export default class GameManager extends Frame.ManagerBase {
    protected _managerName: string = "游戏管理";

    constructor() {
        super();

        this._Initialize();
    }

    _Initialize(): void {
        super._Initialize();
    }

    /** 开启物理系统 */
    openPhysics(print: boolean = false, isLockFrame: boolean = true) {
        //开启物理系统
        const physicsManager = cc.director.getPhysicsManager();
        physicsManager.enabled = true;

        // 开启物理步长的设置
        // physicsManager.enabledAccumulator = true;
        // 物理步长，默认 FIXED_TIME_STEP 是 1/60
        // cc.PhysicsManager.FIXED_TIME_STEP = 1 / 60;

        if (isLockFrame) {
            cc.game.setFrameRate(60);
        }
    }
    /** 开启物理系统Debug */
    openPhysicsDebugDrawFlags(print: boolean = false) {
        // 打印信息
        if (print) {
            cc.director.getPhysicsManager().debugDrawFlags =
                //cc.PhysicsManager.DrawBits.e_aabbBit |
                cc.PhysicsManager.DrawBits.e_jointBit |
                cc.PhysicsManager.DrawBits.e_shapeBit;
        }
    }
    /** 开启碰撞系统 */
    openCollision(print: boolean = false) {
        let manager = cc.director.getCollisionManager();
        manager.enabled = print;
    }
    /** 开启碰撞系统Debug */
    openCollisionDebugDraw(print: boolean = false) {
        let manager = cc.director.getCollisionManager();
        manager.enabledDebugDraw = print;
        // manager.enabledDrawBoundingBox = print;
    }
    /** 开启FPS Debug */
    openDebugFPS(displayStats: boolean = false) {
        cc.debug.setDisplayStats(displayStats);
    }

    private m_CurrMode: GamModeProperty = undefined;
    get currMode() { return this.m_CurrMode; }

    async enterMode(mode: GAME_MODE,showloading = true) {
        if(showloading){
            sers().uiSrv.showLoding();
        }

        let currMode = gameMode_Json()[mode];
        console.time("进入模式耗时---" + currMode.explain);

        this.m_CurrMode = currMode;

        if (!currMode.bundle && currMode.bundleName && currMode.bundleName != "") {
            console.log("加载模式所需资源bundle---", currMode.bundleName);
            await new Promise<void>((resolve, reject) => {
                cc.assetManager.loadBundle(currMode.bundleName, (err: Error, bundle: cc.AssetManager.Bundle) => {
                    if (err) {
                        console.error("进入模式 :" + currMode.explain + ",  加载所需bundle失败", err);
                        reject();
                        return;
                    }
                    currMode.bundle = bundle;
                    cc.director.preloadScene(currMode.sceneName);
                    this.goToNeedEnterMode().then(resolve);
                });
            });
        } else {
            return this.goToNeedEnterMode();
        }
    }

    private async goToNeedEnterMode() {
        sers().uiSrv._closeCurrScenePage();

        await UMe.loadScene(this.m_CurrMode.sceneName);
        sers().uiSrv.hideLoding();
        console.timeEnd("进入模式耗时---" + this.m_CurrMode.explain);
    }

    getCurrModeBundle() {
        return this.m_CurrMode?.bundle;
    }

    backHomePage() {
        // sers().uiSrv.showLoding();
        this.m_CurrMode = undefined;

        sers().uiSrv._closeCurrScenePage();
        UMe.loadScene("home").then(() => {
            sers().uiSrv.hideLoding();
        });
        console.log("返回主页");
    }

    enableMultiTouch(bool: boolean = true) {
        cc.macro.ENABLE_MULTI_TOUCH = bool;
    }
}